﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace _28数据绑定到单个控件
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        Student stu = null;
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            stu = new Student();
            //txtName.DataContext = num.ToString();
            Binding binding = new Binding();
            binding.Source = stu;
            binding.Path = new PropertyPath("Num");
            this.txtName.SetBinding(TextBox.TextProperty, binding);
            this.txtName2.SetBinding(TextBox.TextProperty, binding);

            Binding binding2 = new Binding();
            binding2.Source = stu;
            binding2.Path = new PropertyPath("Name");
            this.txtName3.SetBinding(TextBox.TextProperty, binding2);
            this.txtName4.SetBinding(TextBox.TextProperty, binding2);

            //txtName.DataContext = num;
            //Thread th = new Thread(DataThread);
            //th.IsBackground = true;
            //th.Start();
        }

        private void DataThread()
        {
            while(true)
            {
                stu.Num ++;
                //txtName.Dispatcher.Invoke(new Action(() => { txtName.DataContext = num; }));
                //this.Dispatcher.Invoke(new EventHandler(delegate { txtName.DataContext = num; }));
                //this.Dispatcher.Invoke(new Action(() => { txtName.DataContext = num; }));
                Thread.Sleep(1000);
            }
        }

        private void button_Click(object sender,RoutedEventArgs e)
        {
            //new MainWindow().Show();
        }
    }
}
